package com.condroid;

import ioio.lib.api.Uart;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AbsSpinner;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Setting Activity is user interface for settings important
 * parameters for the equipment like setting parity, baudrate
 * stopbits etc for the device you want to connect. 
 * It has some methods which are used to set up the UI like 
 * setSpinerViews etc
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */

public class SettingsActivity extends Activity implements OnItemSelectedListener {
		private Button saveButton, resetButton;
		private TextView settingView;
		private Spinner portin;
		private Spinner baudin;
		private Spinner stopBitsin;
		private Spinner parityin;
		private Spinner portReceive;
		private String portT1;
		public static String filename;
		public static SharedPreferences settingData;
		SharedPreferences.Editor PortTrans;
		SharedPreferences.Editor Baud;
		SharedPreferences.Editor PortReceive;
		SharedPreferences.Editor StopBit;
		SharedPreferences.Editor Parity;
		private int itemPosition;
		private String baud1;
		private String itemselection;
		private String portR1;
		private int baudRate;
	//	private String Parity;
		private String stopBitsONE;
		private int portTransNum;
		private int portRecvNum;
		private int itemport;
		private String itemSelectionselected;
		private int baud;
		private String stBit1;
		private String parity1;
		private Uart.Parity parityUart;
		private String parity;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        setUpViews();
        setUpSpinnerViews();
        settingData = getSharedPreferences(filename,0);
        PortTrans = settingData.edit();
        PortReceive = settingData.edit();
        Baud = settingData.edit();
    	StopBit = settingData.edit();
    	Parity = settingData.edit();
    	
 //   	parity1 = Uart.Parity.NONE;
     
    }
	
	
// This method sets up View for the Setting Activity
	private void setUpViews(){
		saveButton = (Button)findViewById(R.id.settings_save);
		resetButton = (Button)findViewById(R.id.settings_reset);
		settingView = (TextView)findViewById(R.id.textView1);
		
		portin = (Spinner)findViewById(R.id.portSpin);
		ArrayAdapter<CharSequence> portTransmitAdapter = ArrayAdapter.createFromResource(this, R.array.ports_transmit, 
		android.R.layout.simple_spinner_item);
		portTransmitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		ArrayAdapter<String> portAdapter = new ArrayAdapter<String>(SettingsActivity.this, android.R.layout.simple_spinner_item, portArray);
		

		/**
		 * Save button to save configuration in shared preferences or a file
		 * it calls a method called saveConfiguration to save user settings.
		 **/
		
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				saveConfiguration();
				
			
				Toast.makeText(getApplicationContext(), "Your Configuration Saved!!", Toast.LENGTH_SHORT).show();
			}
			
		});
		
		
		/**
		 * Reset button to reset configuration in shared preferences or a file
		 **/			
		resetButton.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							PortTrans.putString("PortT1", "");
							PortTrans.commit();
							PortReceive.putString("PortR1", "");
							PortReceive.commit();
							Baud.putString("Baud1", "");
							Baud.commit();
							StopBit.putString("StBit1", "");
							StopBit.commit();
							Parity.putString("Parity1", "");
							Parity.commit();
							Toast.makeText(getApplicationContext(), "Your configuration has been reset!!", Toast.LENGTH_SHORT).show();
						}
					});
		}	
			/**
			 * Saves configurations of the user input and parse it
			 **/
			
			private void saveConfiguration() {
				
				
				PortTrans.putString("PortT1", portT1);
				PortTrans.commit();
				PortReceive.putString("PortR1", portR1);
				PortReceive.commit();
				Baud.putString("Baud1", baud1);
				Baud.commit();
				StopBit.putString("StBit1", stBit1);
				StopBit.commit();
				Parity.putString("Parity1",parity1);
				Parity.commit();
/*(				itemSelectionselected = getSelection();
				itemport = getItemPosition();
					if (itemSelectionselected == "portTransmission"){
						
								portTransNum = Integer.parseInt(settingData.getString("PortT1", "Can't load ports!!"));
								settingView.setText(Integer.toString(portTransNum));
			
				}   else 	if (itemSelectionselected == "portReceiving"){
						
								portRecvNum = Integer.parseInt(settingData.getString("PortR1", "Can't load ports!!"));
								settingView.setText(Integer.toString(portRecvNum));
							}
							
					
					else 	if (itemSelectionselected == "baud"){
					
								baud = Integer.parseInt(settingData.getString("Baud1", "Can't load ports!!"));
								settingView.setText(Integer.toString(baud));
			
							}
					
					
					else if (itemSelectionselected == "stopBits"){
						
						
								stopBitsONE = settingData.getString("StBit1", "Can't load ports!!");
								settingView.setText(stopBitsONE);
		
						}
					else if (itemSelectionselected == "parity"){
						
								parity = settingData.getString("Parity1", "Can't load ports!!");
								settingView.setText(parity);
		
						 } 
				
			
*/
				finish();
		

		
		
	}

	
			/**
			 * This method is for setting up Spinner views (Drop down Menu)
			 **/
	
	
		private void setUpSpinnerViews() {
				
				// Drop down menu for Ports transmit
			
				portin = (Spinner)findViewById(R.id.portSpin);
				ArrayAdapter<CharSequence> portTransmitAdapter = ArrayAdapter.createFromResource(this, R.array.ports_transmit, 
				android.R.layout.simple_spinner_item);
				portTransmitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//		ArrayAdapter<String> portAdapter = new ArrayAdapter<String>(SettingsActivity.this, android.R.layout.simple_spinner_item, portArray);
				
				portin.setAdapter(portTransmitAdapter);
				portin.setOnItemSelectedListener(this);

				
				// Drop down menu for Ports Receive
				portReceive = (Spinner)findViewById(R.id.portreceiveSpin);
				ArrayAdapter<CharSequence> portReceiveAdapter = ArrayAdapter.createFromResource(this, R.array.ports_receive, 
						android.R.layout.simple_spinner_item);
				portReceiveAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				//		ArrayAdapter<String> portAdapter = new ArrayAdapter<String>(SettingsActivity.this, android.R.layout.simple_spinner_item, portArray);
				
				portReceive.setAdapter(portReceiveAdapter);
				portReceive.setOnItemSelectedListener(this);

				
				// Drop down menu for baud Rate
		
				baudin = (Spinner)findViewById(R.id.bRateSpin);
				ArrayAdapter<CharSequence> baudAdapter = ArrayAdapter.createFromResource(this, R.array.Baudrate, 
						android.R.layout.simple_spinner_item);
				baudAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				baudin.setAdapter(baudAdapter);
				baudin.setOnItemSelectedListener(this);

				
				// Drop down menu for Stopbits
		
				stopBitsin = (Spinner)findViewById(R.id.sBitSpin);
				ArrayAdapter<CharSequence> stopBitAdapter = ArrayAdapter.createFromResource(this, R.array.StopBits, 
						android.R.layout.simple_spinner_item);
				stopBitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				stopBitsin.setAdapter(stopBitAdapter);
				stopBitsin.setOnItemSelectedListener(this);

				// Drop down menu for Parity
		
				parityin = (Spinner)findViewById(R.id.paritySpin);
				ArrayAdapter<CharSequence> parityAdapter = ArrayAdapter.createFromResource(this, R.array.Parity, 
						android.R.layout.simple_spinner_item);
				parityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				parityin.setAdapter(parityAdapter);
				parityin.setOnItemSelectedListener(this);

		}
		
		
		
		
		/**
		 * This method needs to be override because the class
		 * implements OnItemSelectedListener
		 * This is the method to store the values which has been 
		 * selected by the user
		 **/
	

		@Override
		public void onItemSelected(AdapterView<?> items, View arg1, int pos, long arg3) {
				
			setPosition(pos);
			switch(items.getId()){
				case R.id.portSpin:
					setSelection("portTransmission");
					
					switch(pos){
						case 0:
							portT1 = items.getItemAtPosition(pos).toString();
					//		PortTrans.putString("PortT1", portT1);
					//		PortTrans.commit();
							break;
						case 1:
							portT1 = items.getItemAtPosition(pos).toString();
					//		PortTrans.putString("PortT1", portT1);
					//		PortTrans.commit();
							break;
	
						case 2:
							portT1 = items.getItemAtPosition(pos).toString();
					//		PortTrans.putString("PortT1",portT1);
					//		PortTrans.commit();
							break;
						case 3:
							portT1 = items.getItemAtPosition(pos).toString();
							PortTrans.putString("PortT1",portT1);
							PortTrans.commit();
							break;
						
						}
				break;
				case R.id.portreceiveSpin:
					setSelection("portReceiving");
					switch(pos){
						case 0:
							portR1 = items.getItemAtPosition(pos).toString();
					//		PortReceive.putString("PortR1", portR1);
					//		PortReceive.commit();
							break;
						case 1:
							portR1 = items.getItemAtPosition(pos).toString();
					//		PortReceive.putString("PortR1", portR1);
					//		PortReceive.commit();
							break;
							
						case 2:
							portR1 = items.getItemAtPosition(pos).toString();
					//		PortReceive.putString("PortR1",portR1);
					//		PortReceive.commit();
							break;
						case 3:
							portR1 = items.getItemAtPosition(pos).toString();
					//		PortReceive.putString("PortR1",portR1);
					//		PortReceive.commit();
							break;
							
					}
				break;
				case R.id.bRateSpin:
					setSelection("baud");
					baud1 = items.getItemAtPosition(pos).toString();
//					switch(pos){
//						case 0:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//						case 1:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//	
//						case 2:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1",baud1);
//					//		Baud.commit();
//							break;
//						case 3:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//						case 4:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//							
//						case 5:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1",baud1);
//					//		Baud.commit();
//							break;
//						case 6:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//						case 7:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1", baud1);
//					//		Baud.commit();
//							break;
//							
//						case 8:
//							baud1 = items.getItemAtPosition(pos).toString();
//					//		Baud.putString("Baud1",baud1);
//					//		Baud.commit();
//							break;
//					
//						}
				break;
				case R.id.sBitSpin:
					setSelection("stopBits");
					switch(pos){
						case 0:
							stBit1 = items.getItemAtPosition(pos).toString();
				//			StopBit.putString("StBit1", stBit1);
				//			StopBit.commit();
							break;
						case 1:
							stBit1 = items.getItemAtPosition(pos).toString();
				//			StopBit.putString("StBit1", stBit1);
				//			StopBit.commit();
							break;
							
							
					}
				break;
				case R.id.paritySpin:
					setSelection("parity");
					switch(pos){
						case 0:
							parity1 = items.getItemAtPosition(pos).toString();
					//		Parity.putString("Parity1", parity1);
					//		Parity.commit();
							break;
						case 1:
							parity1 = items.getItemAtPosition(pos).toString();
					//		Parity.putString("Parity1", parity1);
					//		Parity.commit();
							break;
							
						case 2:
							parity1 = items.getItemAtPosition(pos).toString();
					//		Parity.putString("Parity1",parity1);
					//		Parity.commit();
							break;
							
						}
				break;
				
				
				
				
				
				}
				
					
		}
		
		
		/**
		 * Set selection of the item which spinner is selected in spinner view 
		 * (like baud,port or parity)
		 * @param String
		 * @return
		 * */


		private void setSelection(String b) {
			itemselection = b;
					
		}
		/**
		 * Get selection of the item which spinner is selected in spinner view (like baud,port or parity)
		 * this method can be left if there is no need
		 * @param 
		 * @return String (selection)
		 * */

		private String getSelection() {
			return itemselection;
		}
		
		/**
		 * Set position of the item which is selected in a specific spinner view
		 * @param pos 
		 * @return
		 * */

		
		private void setPosition(int pos) {
					itemPosition = pos;
		}


		/**
		 * Get position of the item which is selected in a specific spinner view
		 * this method can be left if there is no need
		 * @param 
		 * @return int (position of item)
		 * */

		private int getItemPosition() {
			return 	itemPosition;			
		}
		
		/**
		 * Give privileges to other classes and activity to use these shared 
		 * preferences (This method needs to be called in order to use any
		 * of the methods in this class).
		 * @param context
		 * @return
		 */
		public static String getConfiguration(Context context){
			
			settingData = PreferenceManager.getDefaultSharedPreferences(context);
			settingData = context.getSharedPreferences(StdbyActivity.stdByFile,0);
			
	//		temp=settingData.getString("Parity2", "Not able to Load");
			return null;
		}	



		
		/**
		 * Override method when  nothing is selected
		 * use it if need!!	
		 */
		
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
			
	
	
	
	
	

}
